package br.uvv.Util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.activation.DataSource;

public class InputStreamDataSource implements DataSource {
	private final String name;
	private final String contentType;
	private final ByteArrayOutputStream baos;

	InputStreamDataSource(final String name, final String contentType, final InputStream inputStream) throws IOException {
		int read;
		this.name = name;
		this.contentType = contentType;

		this.baos = new ByteArrayOutputStream( );

		final byte[] buff = new byte[256];
		while (( read = inputStream.read( buff ) ) != -1) {
			this.baos.write( buff, 0, read );
		}
	}

	public String getContentType() {
		return this.contentType;
	}
	public InputStream getInputStream() throws IOException {
		return new ByteArrayInputStream( this.baos.toByteArray( ) );
	}
	public String getName() {
		return this.name;
	}
	public OutputStream getOutputStream() throws IOException {
		throw new IOException( "Cannot write to this read-only resource" );
	}
}